package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询销量排名top10接口
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO reportTpo10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        Map<String, Object> map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);
        //当前集合存放从begin从end范围的temp集合
        List<GoodsSalesDTO> goodsSalesDTOS = orderMapper.findByMapSetmeal(map);
        List<String> names = goodsSalesDTOS.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers = goodsSalesDTOS.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");
        String numberList = StringUtils.join(numbers, ",");
        return SalesTop10ReportVO
                .builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }

    /**
     * 营业额统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO reportTurnover(LocalDate begin, LocalDate end) {
        //当前集合存放从begin到end范围内的每天日期
        List<LocalDate> dateList = new ArrayList<>();
        //当前集合存放每天的营业额
        List<Double> turnoverList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            //日期计算,计算指定日期的后一天的日期
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天的营业额
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    /**
     * 用户统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO reportUser(LocalDate begin, LocalDate end) {
        //当前集合存放从begin到end范围内的每天日期
        List<LocalDate> dateList = new ArrayList<>();
        //当前集合存放从begin到end范围内的每天新增的用户数量
        List<Long> newUserList = new ArrayList<>();
        //当前集合存放从begin到end范围内的每天用户的总数量
        List<Long> totalUserList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            //日期计算,计算指定日期的后一天的日期
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate date : dateList) {
            Map<String, Object> map = new HashMap<>();
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            map.put("begin", beginTime);
            map.put("end", endTime);
            //搜索每天新增用户数量
            Long numberNew = userMapper.findByMap(map);
            newUserList.add(numberNew);
            //查询用户总量
            Long numberTotal = userMapper.findByMapTotal(map);
            totalUserList.add(numberTotal);
        }

        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .build();
    }

    /**
     * 订单统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO reportOrders(LocalDate begin, LocalDate end) {
        //当前集合存放从begin到end范围内的每天日期
        List<LocalDate> dateList = new ArrayList<>();
        //当前集合存放从begin到end范围内每天的订单数
        List<Long> orderCountList = new ArrayList<>();
        //当前集合存放从begin到end范围内每天的有效订单数
        List<Long> validOrderCountList = new ArrayList<>();

        dateList.add(begin);
        while (!begin.equals(end)) {
            //日期计算,计算指定日期的后一天的日期
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        for (LocalDate date : dateList) {
            Map<String, Object> map = new HashMap<>();
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            map.put("begin", beginTime);
            map.put("end", endTime);
            //记录每天的订单数
            Long orderCount = orderMapper.findOrderCount(map);
            orderCountList.add(orderCount);
            //记录每天的有效订单数
            Long validOrderCount = orderMapper.findValidOrderCount(map);
            validOrderCountList.add(validOrderCount);
        }
        Map map = new HashMap<>();
        Long orderCountTotal = orderMapper.findOrderCount(map);
        Long validOrderCountTotal = orderMapper.findValidOrderCount(map);
        //判断有效订单是否空,为空不进行计算
        Double orderCompletionRate1 = null;
        if (validOrderCountTotal != null && validOrderCountTotal != 0) {
            orderCompletionRate1 = (double) (orderCountTotal / validOrderCountTotal);
        }

        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCompletionRate(orderCompletionRate1)
                .totalOrderCount(Math.toIntExact(orderCountTotal))
                .validOrderCount(Math.toIntExact(validOrderCountTotal))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .build();
    }


}
